package org.lex.ftp.queue;

import java.util.Iterator;
import javax.swing.table.AbstractTableModel;

/*
 * ������е�����ʵ�ڸ��ӣ����ڻ�û���һ�����ʵ���ݽṹ����ʾ<br><br>
 * 
 * ��ʾ������е���ݽṹ�������������Լ��<br>
 * 1��(�����)�߳�ͬ�����������������(��Ȼ���߳�����ʱ���ܻ����)<br>
 * 2��(��ѡ��)֧�ֿ��ٵ������ʺ��޸ģ��ڶ���˫�˲��롢ɾ��Ԫ�ض�Ҫ��<br>
 * 3��(��ѡ��)��ö��д�С��size()����Ӧ�ÿ���(ConcurrentLinkedList.size()����һ������)
 */
public class TransferQueue extends AbstractTableModel implements ITransferQueue {
	public static final String[] COLUMN_NAMES = { "Source", "Target", "Size", "Description" };
	private IQueueContainer<Task> queue = null;

	public TransferQueue() {
		super();
		this.queue = ListContainer.vector();
	}

	/***************************************************************************
	 * implements ITransferQueue
	 **************************************************************************/
	// get
	public Task getTask(int row) {
		return this.queue.get(row);
	}

	public Task getFirst() {
		return this.queue.getFirst();
	}

	public boolean isEmpty() {
		return this.queue.isEmpty();
	}

	public Iterator<Task> iterator() {
		return this.queue.iterator();
	}

	// add
	public <T extends Task> void addLast(T... tasks) {
		for (Task t : tasks)
			this.queue.addLast(t);
		this.fireTableDataChanged();
	}

	public <T extends Task> void addFirst(T... tasks) {
		for (int i = tasks.length - 1; i >= 0; i--)
			this.queue.addFirst(tasks[i]);
		this.fireTableDataChanged();
	}

	// edit
	public void moveTop(int index) {
		QueueContainerUtils.swap(queue, index, 0);
		this.fireTableDataChanged();
	}

	public void moveUp(int index) {
		QueueContainerUtils.swap(queue, index, index - 1);
		this.fireTableDataChanged();
	}

	public void moveDown(int index) {
		QueueContainerUtils.swap(queue, index, index + 1);
		this.fireTableDataChanged();
	}

	public void moveBottom(int index) {
		QueueContainerUtils.swap(queue, index, queue.size() - 1);
		this.fireTableDataChanged();
	}

	// remove
	public Task removeFirst() {
		Task task = this.queue.removeFirst();
		this.fireTableDataChanged();
		return task;
	}

	public void remove(int[] rows) {
		this.queue.remove(rows);
		this.fireTableDataChanged();
	}

	public void clear() {
		this.queue.clear();
		this.fireTableDataChanged();
	}

	// fire
	public void fireQueueChanged(int index) {
		this.fireTableRowsUpdated(index, index);
	}

	public void fireQueueChanged() {
		this.fireTableDataChanged();
	}

	/***************************************************************************
	 * implements TableModel
	 **************************************************************************/
	public String getColumnName(int column) {
		return COLUMN_NAMES[column];
	}

	public int getColumnCount() {
		return COLUMN_NAMES.length;
	}

	public int getRowCount() {
		return this.queue.size();
	}

	public Object getValueAt(int row, int column) {
		if (row >= this.queue.size())
			return null;
		Task task = queue.get(row);
		switch (column) {
		case 0:
			// source
			return task;
		case 1:
			// target
			return task.getDestination();
		case 2:
			// size
			return task;
		case 3:
			// description
			return task.getDescription();
		default:
			return "";
		}
	}

	public String toString() {
		return this.queue.toString();
	}
}
